package com.chinairi.mall.service.impl;

import com.chinairi.mall.controller.viewobject.HomeAllClassVO;
import com.chinairi.mall.model.HomeAllClass;
import com.chinairi.mall.dao.HomeAllClassMapper;
import com.chinairi.mall.service.IHomeAllClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-10-14
 */
@Service
public class HomeAllClassServiceImpl extends ServiceImpl<HomeAllClassMapper, HomeAllClass> implements IHomeAllClassService {

    @Autowired
    private HomeAllClassMapper homeAllClassMapper;
    /**
     * 获取首页商品分类
     *
     * @return
     */
    @Override
    public List<HomeAllClassVO> getHomeAllClass() {
        List<HomeAllClassVO> homeAllClassVOList = homeAllClassMapper.selectHomeAllClass();
        return homeAllClassVOList.stream().filter(vo -> vo.getHomeAllClassLevel() == 1)
                .peek(vo -> vo.setHomeAllClassVOList(getChildList(vo, homeAllClassVOList)))
                .sorted(Comparator.comparing(HomeAllClassVO::getHomeAllClassCode)).collect(Collectors.toList());
    }

    /**
     * 分级获取子类
     * @param homeAllClassVO
     * @param list
     * @return
     */
    private List<HomeAllClassVO> getChildList(HomeAllClassVO homeAllClassVO, List<HomeAllClassVO> list) {
        return list.stream().filter(vo -> vo.getHomeAllClassParentDbid()!= null && vo.getHomeAllClassParentDbid().equals(homeAllClassVO.getHomeAllClassDbid()))
                .peek(vo -> vo.setHomeAllClassVOList(getChildList(vo, list)))
                .collect(Collectors.toList());
    }
}
